package com.fzj.example.collection;

import java.util.Iterator;
import java.util.Stack;
import java.util.Vector;

/**
 * ClassName: MyVectorStack
 * date: 2019/10/12 10:20
 * Description: TODO
 * @author fanzj@bonree.com
 * @since JDK 1.8
 */
public class MyVectorStack {
    public static void main(String[] args) {

        /*********************************************Vector**********************************/
        //初始化容量是10,扩容默认是10
        Vector<String> vector=new Vector<>();
        //初始化容量是20，扩容默认是20
        Vector<String> vector1=new Vector<>(20);
        //初始化容量是10，扩容配置每次是20
        Vector<String> vector2=new Vector<>(10,20);
        int i=0;
        while (i<10){
            i++;
            vector.add("字符串"+i);
            vector1.add("字符串vertor1"+i);
        }
        //toString-AbstractCollection
        System.out.println(vector.toString());
        //扩容方式1
        vector.setSize(20);
        System.out.println("第一个元素0开始:"+vector.get(0));
        System.out.println("第11个元素:"+vector.get(10));
        //无需扩容，拿取的是elementCount=10
        System.out.println("容器规格elementCount:"+vector.size());
        System.out.println("容器规格elementData:"+vector.capacity());
        //扩容方式1
        //还原,elementData
        vector.setSize(10);
        vector.add("字符串"+11);
        System.out.println("第一个元素0开始:"+vector.get(0));
        System.out.println("第11个元素:"+vector.get(10));
        //拿取的是elementCount++
        System.out.println("容器规格elementCount:"+vector.size());
        System.out.println("容器规格elementData:"+vector.capacity());
        //没有setSize
        vector1.add("字符串"+11);
        System.out.println("第一个元素0开始:"+vector1.get(0));
        System.out.println("第11个元素:"+vector1.get(10));
        //拿取的是elementCount++
        System.out.println("容器规格elementCount:"+vector1.size());
        System.out.println("容器规格elementData:"+vector1.capacity());
        //
        vector.add(1,"修改第二个位置的字符串");
        vector.add("增加字符串**");
        vector.set(2,"修改地三个位置的字符串");
        //清除一个
        vector.remove(3);
        //借助indexOf，然后equals方法比较,清除也是1个，不是遍历equals
        vector.add("字符串5");
        //顺时索引查询-0开始，返回正确位置
        System.out.println("indexOf:"+ vector.indexOf("字符串5"));
        //顺时索引查询-6开始，返回正确位置
        System.out.println("indexOf:"+ vector.indexOf("字符串5",6));
        //顺时索引0，倒置查询，返回正确位置
        System.out.println("lastIndexOf:"+ vector.lastIndexOf("字符串5"));
        //顺时索引5，倒置查询，返回正确位置
        System.out.println("lastIndexOf:"+ vector.lastIndexOf("字符串5",5));
        System.out.println("操作之后："+vector.toString());
        vector.remove("字符串5");
        System.out.println("操作之后：元素获取:"+vector.get(0));
        System.out.println("操作之后：元素获取:"+vector.indexOf("字符串6"));
        System.out.println("操作之后：元素获取:"+vector.elementAt(7));
        //合并过来，返回boolean
        vector.addAll(vector1);
        System.out.println("操作之后："+vector.toString());
        //遍历
        Iterator<String> itrV=vector.iterator();
        Iterator<String> itrVs=vector.listIterator();
        while (itrV.hasNext()){
            System.out.print(itrV.next()+",");
        }

        /*********************************stack**************************************/
        Stack<String> stack = new Stack<>();
        for(int j= 0; j < 10; j++){
            stack.add("字符串"+j);
        }

        System.out.println("statck"+stack);

        System.out.println("peek"+stack.peek());

        stack.push("测试");

        System.out.println("statck"+stack);

        System.out.println("pop"+stack.pop());

        System.out.println("statck"+stack);

        System.out.println("empty"+stack.empty());

        System.out.println("search"+stack.search("字符串3"));

        System.out.println("stack遍历：");
        while(!stack.empty()){
            System.out.print(stack.pop() + " ");
        }
        //发现size为0
        Iterator<String> itrS=stack.iterator();
        while (itrS.hasNext()){
            System.out.println(itrS.next());
        }
    }
}
